﻿using System;

namespace MathService.DSP
{
    public class AnalogPrototypedFilter : CBIH
    {
        public static double FriquencyAnalog2DigitalConversion(double DigitalF, double dt)
        {
            return 2 * Math.Tan(DigitalF / 2) / dt;
        }

        public static double FriquencyDigital2AnalogConversion(double AnalogF, double dt)
        {
            return Math.Atan(AnalogF * dt / 2) * 2;
        }

        public PQ Hp { get; private set; }
        public double dt { get; private set; }

        public AnalogPrototypedFilter(PQ Hp, double dt)
            : this(Hp.P2Z_BilinearTransform(dt))
        {
            this.Hp = Hp;
            this.dt = dt;
        }

        private AnalogPrototypedFilter(PQ Hz) : base(Hz.A, Hz.B) { }

        public override Complex TransferFunction(double f, double dt)
        {
            return Hp.Value(Complex.Exp(Consts.pi2 * f));
        }
    }

    //public abstract class RC_Filter : AnalogPrototypedFilter
    //{
    //    protected RC_Filter(double tau, double dt) : base(HFunction(tau).P2Z_BilinearTransform(dt), dt) { }

    //    protected abstract PQ HFunction(double tau);
    //}

    public class RC_LowPassFilter : AnalogPrototypedFilter
    {
        private static PQ GetFunction(double tau)
        {
            return new PQ(new Polynom(1), new Polynom(1, tau));
        }

        public double Tau { get; private set; }

        public RC_LowPassFilter(double R, double C, double dt) : this(R * C, dt) { }
        public RC_LowPassFilter(double tau, double dt) : base(GetFunction(tau), dt) { Tau = tau; }
    }

    public class RC_HiPassFilter : AnalogPrototypedFilter
    {
        private static PQ GetFunction(double tau)
        {
            return new PQ(new Polynom(0, tau), new Polynom(1, tau));
        }

        public double Tau { get; private set; }

        public RC_HiPassFilter(double R, double C, double dt) : this(R * C, dt) { }
        public RC_HiPassFilter(double tau, double dt) : base(GetFunction(tau), dt) { Tau = tau; }
    }

    public class LC_Filter
    {

    }
}